package com.andy.note.main.sort;

/**
 * 选择排序
 * @author Andy
 * 参考文章
 * https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247483824&idx=1&sn=15cdb8ac18546ae005140233ad4800bb&chksm=ebd63e9cdca1b78a7f664f7f8e3ec1760fa027f0c84427aaa2dfa4cdafa862c7b1f8ba88480b&scene=21#wechat_redirect 
 *
 */
public class SelectionSort {
    public void selectionSort(int[] array) {
       int temp;
       for (int i = 0; i < array.length - 1; i++) {
              for (int j = i + 1; j <= array.length - 1; j++) {
              if (array[i] > array[j]) {    
              // 注意和冒泡排序的区别，这里是i和j比较。
              temp = array[i];
              array[i] = array[j];
              array[j] = temp;
             }
        }            // 打印每趟排序结果
       for (int m = 0; m <= array.length - 1; m++) {
             System.out.print(array[m] + "\t");
       }
          System.out.println();
       }
    }    
    public static void main(String[] args) {
        SelectionSort selectionSort = new SelectionSort(); 
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        selectionSort.selectionSort(array);        
    for (int m = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}